<?php
class categoryModel{

	var $cat_id     		= 0;
	var $title				= null;
	var $status 			= 0;
	var $create_datetime 	= 0;
	var $modify_datetime 	= 0;
	var $alias 				= null;
	var $keyword 			= null;
	var $description		= null;
	var $parent_id		    = 0;

	/**
	 * Thêm / Sửa dữ liệu
	 * @return boolean
	 */
	function store(){
		global $db;
		if($this->cat_id == 0){
			$field[] = (int) $this->cat_id;
			$field[] = $db->quote($this->title);
			$field[] = $db->quote($this->description);
			$field[] = time();
			$field[] = time();
			$field[] = (int) $this->status;
			$field[] = (int) $this->parent_id;
			$field[] = $db->quote($this->alias);
			$field[] = $db->quote($this->keyword);
			$query = "INSERT INTO ".TBL_CATEGORIES." VALUES(".implode(',', $field).")";
			if(!$db->query($query)) return false;
			return true;
		}else{
			$field[] = $db->quoteField('title')		.' = '.$db->quote($this->title);
			$field[] = $db->quoteField('status')	.' = '.(int) $this->status;
			$field[] = $db->quoteField('modify_datetime')	.' = '.time();
			$field[] = $db->quoteField('alias')	.' = '.$db->quote($this->alias);
			$field[] = $db->quoteField('keyword')	.' = '.$db->quote($this->keyword);
			$field[] = $db->quoteField('description')	.' = '.$db->quote($this->description);
			$field[] = $db->quoteField('parent_id')	.' = '.(int) $this->parent_id;

			$query = "UPDATE ".TBL_CATEGORIES.
					 " SET ".implode(',', $field).
					 " WHERE cat_id = ".(int) $this->cat_id;
			if(!$db->query($query)) return false;
			return true;
		}
	}

	/**
	 * Lấy dữ liệu theo id
	 * @return boolean
	 */
	function read(){
		global $db;
		$query = "SELECT * FROM ".TBL_CATEGORIES." WHERE cat_id = ".(int) $this->cat_id;
		$data = $db->get_row($query);
		if($data){
			$this->cat_id = $data->cat_id;
			$this->title = $data->title;
			$this->status = $data->status;
			$this->create_datetime = $data->create_datetime;
			$this->modify_datetime = $data->modify_datetime;
			$this->alias = $data->alias;
			$this->keyword = $data->keyword;
			$this->description = $data->description;
			$this->parent_id = $data->parent_id;
		}else{
			return false;
		}

	}


	/**
	 * Lấy dữ liệu trong bảng categories
	 * @param array $param
	 * @return boolean|array
	 */
	function get_all($param = array('limit'=>20,'offset'=>0,'field'=>'*','order'=>'DESC')){
		global $db;

		$query  = "SELECT ".$param['field']." FROM ".TBL_VIEW_CATEGORIES.
		$query .= $this->filter($param);
		$query .= " LIMIT ".(int) $param['offset'].",".(int) $param['limit'];
		$data = $db->get_results($query);
		if(!$data) return false;
		return $data;
	}

	/**
	 * Đếm số bản ghi
	 * @param array $param
	 * @return boolean|array
	 */
	function total_rows($param = array('search'=>null,'field'=>'*')){
		global $db;
		$query = "SELECT COUNT(".$param['field'].") FROM ".TBL_VIEW_CATEGORIES;
		$query .= $this->filter($param);
		$data = $db->get_var($query);
		if(!$data) return false;
		return $data;
	}
	
	/**
	 * Lọc theo tham số
	 * @param array $param
	 * @return string
	 */
	function filter($param){
		global $db;
		$query = $order = '';
		$query .= (isset($param['search']) && !empty($param['search'])) ? $this->where($query).'(title LIKE '.$db->quote('%'.$param['search'].'%').' OR cat_id LIKE '.$db->quote('%'.$param['search'].'%').')' : '';
		
		//Lọc theo id
		switch ($param['cb_id']){
			case 0:
				$order[] = ' cat_id ASC';
				break;	
			default:
			case 1:
				$order[] = ' cat_id DESC';
				break;
		}
		
		//Lọc theo trạng thái
		switch ($param['cb_status']){
			case 0:
				$query .= $this->where($query).'status = 0';
				break;
			case 1:
				$query .= $this->where($query).'status = 1';
				break;
		}
		
		if(is_array($order)){
			$query .= ' ORDER BY'.implode(',', $order);	
		}
		
		return $query;	
	}
	
	/**
	 * Kiểm tra xem có WHERE trong truy vấn chưa
	 * @param string $query
	 * @return string
	 */
	function where($query){
		return (strpos($query, 'WHERE') === false) ? ' WHERE ' : ' AND ';	
	}

	/**
	 * Xóa một bản ghi theo id
	 * @return boolean
	 */
	function delete(){
		global $db;
		$query = "DELETE FROM ".TBL_CATEGORIES." WHERE user_id = ".(int) $this->cat_id;
		if(!$db->query($query)) return false;
		return true;
	}

	/**
	 * xóa nhiều bản ghi
	 * @param array $id
	 * @return boolean
	 */
	function delete_all(array $id){
		global $db;
		$query = "DELETE FROM ".TBL_CATEGORIES." WHERE user_id IN (".implode(',', $id).")";
		if(!$db->query($query)) return false;
		return true;
	}

	/**
	 * Thay đổi trạng thái một bản ghi
	 * @param int $status
	 * @param int $id
	 * @return boolean
	 */
	function change_status($status,$id){
		global $db;
		$query = "UPDATE ".TBL_CATEGORIES." SET status = ".(int) $status.
				 " WHERE cat_id = ".(int) $id;
		if(!$db->query($query)) return false;
		return true;
	}

	/**
	 * Thay đổi trạng thái nhiêu bản ghi
	 * @param int $status
	 * @param array $id
	 * @return boolean
	 */
	function change_status_all($status,array $id){
		global $db;
		$query = "UPDATE ".TBL_CATEGORIES." SET status = ".(int) $status.
				 " WHERE cat_id IN (".implode(',', $id).")";
		if(!$db->query($query)) return false;
		return true;
	}

	/**
	 * Kiểm tra alias đã có chưa
	 * @param string $alias
	 * @return boolean
	 */
	function check_alias_exit($alias){
		global $db;
		$query = "SELECT COUNT(*) FROM ".TBL_CATEGORIES." WHERE alias = ".$db->quote($alias);
		$data = $db->get_var($query);
		if(!$data) return false;
		return $data;
	}
	
	/**
	 * Lấy id theo alias
	 * @return boolean|NULL
	 */
	function last_insert_id(){
		global $db;
		$query = "SELECT cat_id FROM ".TBL_CATEGORIES." WHERE alias = ".$db->quote($this->alias);
		$data = $db->get_var($query);
		if(!$data) return false;
		return $data;
	}
	
	/**
	 * Lấy all category
	 * @return boolean|array
	 */
	function get_all_categories(){
		global $db;

		$query = "SELECT cat_id,title,parent_id FROM ".TBL_CATEGORIES.
				 " ORDER BY cat_id ASC ";
		$data = $db->get_results($query);
		if(!$data) return false;
		return $data;
	}
}